package com.quwan.manager.controller;

import com.quwan.common.BaseController;
import com.quwan.common.result.ApiResult;
import com.quwan.common.result.PageResult;
import com.quwan.entity.TCircleType;
import com.quwan.manager.service.ICircleTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author laozhang
 * @date 2022/3/17
 */
@Api(tags = "后台圈子类型管理接口")
@RestController
@RequiredArgsConstructor
@RequestMapping("mgr/circleType")
public class CircleTypeController extends BaseController {

    private final ICircleTypeService circleTypeService;


    @ApiOperation(value = "圈子类型列表分页",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("circleTypeListPage")
    public ApiResult<PageResult<TCircleType>> CircleTypeListPage(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            @ApiParam(value="状态 1:正常,2:禁用",required = false) @RequestParam(required = false) Integer status,
            @ApiParam(value="名称",required = false) @RequestParam(required = false) String name) {
        PageResult<TCircleType> res = circleTypeService.getCircleTypeListPage(page, size, name, status);
        return ApiResult.success(res);
    }

    @ApiOperation(value = "圈子类型列表",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("circleTypeList")
    public ApiResult<List<TCircleType>> CircleTypeList(
            @ApiParam(value="状态 1:正常,2:禁用",required = false) @RequestParam(required = false) Integer status,
            @ApiParam(value="名称",required = false) @RequestParam(required = false) String name) {
        List<TCircleType> res = circleTypeService.getCircleTypeList(name, status);
        return ApiResult.success(res);
    }

    @ApiOperation("圈子类型修改——不需要传时间字段")
    @PostMapping("updateCircleType")
    public ApiResult updateCircleType(HttpServletRequest request,TCircleType circleType){
        circleType.setLastModifyTime(LocalDateTime.now());
        boolean success = circleTypeService.updateCircleType(circleType);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @PostMapping("addCircleType")
    @ApiOperation("添加圈子类型——不需要传时间字段")
    public ApiResult addCircleType(HttpServletRequest request,TCircleType circleType){
        TokenInfo tokenInfo = getTokenInfo(request);
        circleType.setCreateTime(LocalDateTime.now());
        circleType.setCreateUserName(tokenInfo.getUsername());
        boolean success = circleTypeService.addCircleType(circleType);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }

    @ApiOperation(value = "圈子类型详情",notes = "<span style=\"color:red\">注意参数是否必填</span>",httpMethod = "POST")
    @PostMapping("detailCircleType")
    public ApiResult detailCircleType(@RequestParam(value = "id") Integer id) {
        TCircleType CircleType = circleTypeService.detailCircleType(id);
        return ApiResult.success(CircleType);

    }

    @PostMapping("delCircleType")
    @ApiOperation("删除圈子类型")
    public ApiResult delCircleType(@RequestParam(value = "id") Integer id){
        boolean success = circleTypeService.delCircleType(id);
        if (success)
            return ApiResult.successMsg();
        else
            return ApiResult.failed();
    }


}
